//
//  ServiceVC.m
//  NewProject
//
//  Created by 李航飞 on 2017/6/4.
//  Copyright © 2017年 李航飞. All rights reserved.
//

#import "ServiceVC.h"
#import "ServiceTableViewCell.h"
#import "MergeViewController.h"
#import "EnsureViewController.h"
@interface ServiceVC ()<UITableViewDelegate,UITableViewDataSource,ServiceTableViewCellDelegate>
@property(nonatomic, strong)UIView *headView;
@property(nonatomic, strong)UITableView *tableView;
@property(nonatomic, copy)NSArray *dataArray;
@property(nonatomic, copy)NSMutableArray *netDataArray;
@end

@implementation ServiceVC
- (void)httpGetData{
    [[MyNetWorking sharedManager]GET_PATH:@"g=portal&m=buyer&a=cart&buyer_id=1" success:^(id responseObject) {
//        [self.netDataArray addObjectsFromArray:responseObject];
        self.dataArray = responseObject;
        [self createTableView];
    } failure:^(NSError *error) {
        
    }];
}

- (NSMutableArray *)netDataArray{
    if (!_netDataArray) {
        _netDataArray = [[NSMutableArray alloc]init];
    }
    return _netDataArray;
}

- (NSArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [[NSArray alloc]init];
    }
    return _dataArray;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
//    self.tabBarController.tabBar.hidden=NO;

    //self.navigationController.navigationBarHidden=YES;
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
//    self.tabBarController.tabBar.hidden=YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = RGB0X(0xF8F9F6);
    self.navigationItem.title = @"购物车";
    
//    self.dataArray = @[@{kImage:@"商品",kTitle:@"超级无敌大西瓜",kPrice:@"¥5"},@{kImage:@"商品2",kTitle:@"清爽柠檬味汽水",kPrice:@"¥7"},@{kImage:@"商品",kTitle:@"就是瓶雪碧",kPrice:@"¥7"}];
    [self createHeadView];
    [self httpGetData];
//    [self createTableView];
}

- (void)createHeadView{
    _headView = [[UIView alloc]init];
    _headView.frame = CGRectMake(0, 0, WIDTH, 161/WIDTH_6_SCALE);
    _headView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_headView];
    
    UIImageView * bgImageView = [[UIImageView alloc]init];
    bgImageView.image = [UIImage imageNamed:@"栏"];
    [_headView addSubview:bgImageView];
    
    UIImageView * locationImageView= [[UIImageView alloc]init];
    locationImageView.image = [UIImage imageNamed:@"定位"];
    [_headView addSubview:locationImageView];
    
    UILabel *nameLabel = [[UILabel alloc]init];
    nameLabel.font = FONT(15);
    nameLabel.text = @"姓名       18900000000";
    nameLabel.textColor = RGB0X(0x5d5d5d);
    [_headView addSubview:nameLabel];
    
    UILabel *locationLabel = [[UILabel alloc]init];
    locationLabel.font = FONT(15);
    locationLabel.text = @"和泰大厦(西门)1108";
    locationLabel.textColor = RGB0X(0x5d5d5d);
    [_headView addSubview:locationLabel];
    
    UIImageView *lineImageView = [[UIImageView alloc]init];
    lineImageView.image = [UIImage imageNamed:@"线"];
    [_headView addSubview:lineImageView];
    
    UIImageView *lineImageViews = [[UIImageView alloc]init];
    lineImageViews.image = [UIImage imageNamed:@"线"];
    [_headView addSubview:lineImageViews];
    CGAffineTransform transform= CGAffineTransformMakeRotation(M_PI);
    lineImageViews.transform = transform;

    UIImageView *boxImageView = [[UIImageView alloc]init];
    boxImageView.image = [UIImage imageNamed:@"盒"];
    [_headView addSubview:boxImageView];
    
    NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc]initWithString:@"20分钟内妥妥送到\n直达您的家中"];
    
    [AttributedStr addAttribute:NSForegroundColorAttributeName
     
                          value:RGB0X(0xBFBFBF)
     
                          range:NSMakeRange(AttributedStr.length-6, 6)];
    
    UILabel *timeLabel = [[UILabel alloc]init];
    timeLabel.font = FONT(13);
    timeLabel.textAlignment = NSTextAlignmentCenter;
    timeLabel.attributedText = AttributedStr;
    timeLabel.numberOfLines = 2;
    [_headView addSubview:timeLabel];
    
    [bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(0);
        make.top.equalTo(self.view.mas_top).offset(0);
        make.size.mas_equalTo(CGSizeMake(WIDTH, 98/WIDTH_6_SCALE));
    }];
    
    [locationImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(15/WIDTH_6_SCALE);
        make.centerY.equalTo(bgImageView.mas_centerY).offset(0);
        make.size.mas_equalTo(CGSizeMake(20/WIDTH_6_SCALE,24/WIDTH_6_SCALE));
    }];
    
    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(locationImageView.mas_right).offset(16/WIDTH_6_SCALE);
        make.top.equalTo(bgImageView.mas_top).offset(27/WIDTH_6_SCALE);
    }];
    
    [locationLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(nameLabel.mas_left).offset(0);
        make.top.equalTo(nameLabel.mas_bottom).offset(12/WIDTH_6_SCALE);
    }];
    
    [lineImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(50/WIDTH_6_SCALE);
        make.top.equalTo(bgImageView.mas_bottom).offset(26/WIDTH_6_SCALE);
        make.size.mas_equalTo(CGSizeMake(50/WIDTH_6_SCALE,2/WIDTH_6_SCALE));
    }];
    
    [lineImageViews mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view.mas_right).offset(-67/WIDTH_6_SCALE);
        make.top.equalTo(bgImageView.mas_bottom).offset(26/WIDTH_6_SCALE);
        make.size.mas_equalTo(CGSizeMake(50/WIDTH_6_SCALE,2/WIDTH_6_SCALE));
    }];

    [boxImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(110/WIDTH_6_SCALE);
        make.top.equalTo(bgImageView.mas_bottom).offset(15/WIDTH_6_SCALE);
        make.size.mas_equalTo(CGSizeMake(22/WIDTH_6_SCALE,21/WIDTH_6_SCALE));
    }];
    
    [timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(boxImageView.mas_right).offset(6/WIDTH_6_SCALE);
        make.top.equalTo(boxImageView.mas_top).offset(6/WIDTH_6_SCALE);
    }];
}

- (void)createTableView{
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 169/WIDTH_6_SCALE, WIDTH, HIGHT-64-49.5-169/WIDTH_6_SCALE)];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_tableView];
}

#pragma tableView datasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count+3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *CellIdentifier = SF(@"%ld",indexPath.row);
    if (indexPath.row == self.dataArray.count+2) {
        CellIdentifier = @"last";
    }
    ServiceTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[ServiceTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.delegate = self;
    }
    if (indexPath.row>=2&&indexPath.row!=self.dataArray.count+2) {
        cell.model = self.dataArray[indexPath.row-2];
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        return 60/WIDTH_6_SCALE;

    }else if (indexPath.row == 1){
        return 52/WIDTH_6_SCALE;

    }else if (indexPath.row == self.dataArray.count+2){
        return 58/WIDTH_6_SCALE;
    }
    return 83/WIDTH_6_SCALE;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 1) {
        MergeViewController *vc = [[MergeViewController alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
    }
    if (indexPath.row == self.dataArray.count+2) {
    }
}

- (void)Settlement{
    EnsureViewController *vc = [[EnsureViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
